package com.ygqh.baby.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.foxinmy.weixin4j.util.StringUtil;
import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgFunnyVideoMapper;
import com.ygqh.baby.po.YgFunnyVideo;
import com.ygqh.baby.po.YgFunnyVideoExample;
import com.ygqh.baby.service.YgFunnyVideoPraiseService;
import com.ygqh.baby.service.YgFunnyVideoService;
import com.ygqh.baby.utils.InternetWormUtils;

@Service
public class YgFunnyVideoServiceImpl implements YgFunnyVideoService {

	@Autowired
	private YgFunnyVideoMapper ygFunnyVideoMapper;
	@Autowired
	private YgFunnyVideoPraiseService ygFunnyVideoPraiseService;

	@Override
	public List<YgFunnyVideo> find() {
		YgFunnyVideoExample example = new YgFunnyVideoExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygFunnyVideoMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgFunnyVideoExample example = new YgFunnyVideoExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygFunnyVideoMapper.countByExample(example);
	}

	@Override
	public YgFunnyVideo findById(Long id) {
		return ygFunnyVideoMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgFunnyVideo ygFunnyVideo) {
		return ygFunnyVideoMapper.insertSelective(ygFunnyVideo);
	}

	@Override
	public int update(YgFunnyVideo ygFunnyVideo) {
		return ygFunnyVideoMapper.updateByPrimaryKeySelective(ygFunnyVideo);
	}

	@Override
	public ResultSet<YgFunnyVideo> search(QueryInfo queryInfo, String q) {
		List<YgFunnyVideo> list = ygFunnyVideoMapper.selectSuper(queryInfo, q);
		int count = ygFunnyVideoMapper.countSuper(q);
		ResultSet<YgFunnyVideo> resultSet = new ResultSet<YgFunnyVideo>(count, list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgFunnyVideoExample example = new YgFunnyVideoExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgFunnyVideo record = new YgFunnyVideo();
		record.setStatus(DataStatus.Delete);

		return ygFunnyVideoMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(YgFunnyVideo ygFunnyVideo) {
		int r = 0;
		if (ygFunnyVideo.getId() != null) {
			r = this.update(ygFunnyVideo);
		} else {
			r = this.save(ygFunnyVideo);
		}
		return r;
	}

	@Override
	public Message addFunnyVideo(String title, String videoUrl, String imgPath, Long userId, String remark) {
		String videoPath = InternetWormUtils.getVideoPath(videoUrl);
		if (StringUtil.isBlank(videoPath)) {
			return Message.error("请录入正确的视频地址。", null);
		}
		YgFunnyVideo funnyVideo = new YgFunnyVideo(title, imgPath, videoPath, userId);
		funnyVideo.setCreateTime(new Date());
		funnyVideo.setRemark(remark);
		this.save(funnyVideo);
		return Message.success(funnyVideo.getId());
	}

	@Override
	public Message pointPraise(Long videoId, String openId, String nickName, String headUrl) {
		Boolean hasPoint = ygFunnyVideoPraiseService.hasPoint(openId, videoId);
		if (hasPoint) {
			return Message.error("重复点赞", openId);
		}
		int i = ygFunnyVideoMapper.updatePraiseCount(videoId, 1);
		if (i > 0) {
			ygFunnyVideoPraiseService.addPraise(videoId, openId, nickName, headUrl);
		}
		return Message.success(i);
	}

	@Override
	public Message cancelPraise(Long videoId, String userNameOrOpenId) {
		Boolean hasPoint = ygFunnyVideoPraiseService.hasPoint(userNameOrOpenId, videoId);
		if (!hasPoint) {
			return Message.error("并未发生点赞，无法取消", userNameOrOpenId);
		}
		int i = ygFunnyVideoMapper.updatePraiseCount(videoId, -1);
		if (i > 0) {
			ygFunnyVideoPraiseService.deleteSoft(videoId, userNameOrOpenId);
		}
		return Message.success(i);
	}

	@Override
	public List<YgFunnyVideo> findVideoList(QueryInfo queryInfo, Long userId) {
		return ygFunnyVideoMapper.selectVideoList(queryInfo, userId);
	}
}
